import 'package:flutter/material.dart';
import 'package:flutter_test_qq/main.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'dataBase.dart';
class registerPage extends StatefulWidget{

  const registerPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState()=>registerPageState();

}
var qqId = '';
var qqname = '';
var qqpwd = '';
var qqpic = '';
String picUrl = '';

class registerPageState extends State<registerPage>{
  TextEditingController id = new TextEditingController();
  TextEditingController name = new TextEditingController();
  TextEditingController pwd = new TextEditingController();
  TextEditingController pwd2 = new TextEditingController();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: Container(
              padding: EdgeInsets.all(1.0),
              child: Icon(Icons.keyboard_arrow_left),
            ),
            onPressed: (){
              Navigator.of(context).pop();
            },
          ),
          title: Text('QQ注册',style:TextStyle(fontSize: 20,),),
        ),
        body: Column(
          children: [
            SizedBox(height: 10,),
            // 头像上传
            InkWell(
                onTap: () {
                  showModalBottomSheet(
                      context: context,
                      builder: (context) {
                        return Container(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              Text(
                                "请选择头像",
                                style: TextStyle(fontSize: 25),
                              ),
                              Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceEvenly,
                                children: [
                                  InkWell(
                                    onTap: () {
                                      setState(() {
                                        picUrl = "images/pic1.jpg";
                                      });
                                      Navigator.pop(context);
                                    },
                                    child: CircleAvatar(
                                        backgroundImage: AssetImage(
                                            "images/pic1.jpg")),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      setState(() {
                                        picUrl = "images/pic2.jpg";
                                      });
                                      Navigator.pop(context);
                                    },
                                    child: CircleAvatar(
                                        backgroundImage: AssetImage(
                                            "images/pic2.jpg")),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      setState(() {
                                        picUrl = "images/pic3.jpg";
                                      });
                                      Navigator.pop(context);
                                    },
                                    child: CircleAvatar(
                                        backgroundImage: AssetImage(
                                            "images/pic3.jpg")),
                                  ),
                                ],
                              ),
                              Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceEvenly,
                                children: [
                                  InkWell(
                                    onTap: () {
                                      setState(() {
                                        picUrl = "images/pic4.jpg";
                                      });
                                      Navigator.pop(context);
                                    },
                                    child: CircleAvatar(
                                        backgroundImage: AssetImage(
                                            "images/pic4.jpg")),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      setState(() {
                                        picUrl = "images/pic5.jpg";
                                      });
                                      Navigator.pop(context);
                                    },
                                    child: CircleAvatar(
                                        backgroundImage: AssetImage(
                                            "images/pic5.jpg")),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      setState(() {
                                        picUrl = "images/pic6.jpg";
                                      });
                                      Navigator.pop(context);
                                    },
                                    child: CircleAvatar(
                                        backgroundImage: AssetImage(
                                            "images/pic6.jpg")),
                                  ),
                                ],
                              ),
                              Row(
                                mainAxisAlignment:
                                MainAxisAlignment.spaceEvenly,
                                children: [
                                  InkWell(
                                    onTap: () {
                                      setState(() {
                                        picUrl = "images/pic7.jpg";
                                      });
                                      Navigator.pop(context);
                                    },
                                    child: CircleAvatar(
                                        backgroundImage: AssetImage(
                                            "images/pic7.jpg")),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      setState(() {
                                        picUrl = "images/pic8.jpg";
                                      });
                                      Navigator.pop(context);
                                    },
                                    child: CircleAvatar(
                                        backgroundImage: AssetImage(
                                            "images/pic8.jpg")),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      setState(() {
                                        picUrl = "images/pic9.jpg";
                                      });
                                      Navigator.pop(context);
                                    },
                                    child: CircleAvatar(
                                        backgroundImage: AssetImage(
                                            "images/pic9.jpg")),
                                  ),
                                ],
                              )
                            ],
                          ),
                        );
                      });
                },
                child: CircleAvatar(
                  backgroundImage: AssetImage(picUrl),
                  maxRadius: 40,
                )),
            //  qq号输入
            SizedBox(height: 10,),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
              child: SizedBox(
                child: TextField(
                  controller: id,
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(
                    hintText: 'QQ号',
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Color(0x00BB0000)),
                      borderRadius: BorderRadius.all(
                        Radius.circular(15),
                      ),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Color(0x00000000)),
                      borderRadius: BorderRadius.all(
                        Radius.circular(15),
                      ),
                    ),
                  ),
                ),
                width: 340,
              )
            ),
            //  昵称输入
            SizedBox(height: 10,),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
              child: SizedBox(
                child: TextField(
                  controller: name,
                  decoration: InputDecoration(
                    hintText: '昵称',
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Color(0x00BB0000)),
                      borderRadius: BorderRadius.all(
                        Radius.circular(15),
                      ),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Color(0x00000000)),
                      borderRadius: BorderRadius.all(
                        Radius.circular(15),
                      ),
                    ),
                  ),
                ),
                width: 340,
              )
            ),
            //  密码输入
            SizedBox(height: 10,),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
              child: SizedBox(
                child:  TextField(
                  controller: pwd,
                  obscureText: true,
                  decoration: InputDecoration(
                    hintText: '密码',
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Color(0x00BB0000)),
                      borderRadius: BorderRadius.all(
                        Radius.circular(15),
                      ),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Color(0x00000000)),
                      borderRadius: BorderRadius.all(
                        Radius.circular(15),
                      ),
                    ),
                  ),
                ),
                width: 340,
              )
            ),
            //  确认密码
            SizedBox(height: 10,),
            Padding(
              padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
              child: SizedBox(
                child: TextField(
                  controller: pwd2,
                  obscureText: true,
                  decoration: InputDecoration(
                    hintText: '确认密码',
                    filled: true,
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Color(0x00BB0000)),
                      borderRadius: BorderRadius.all(
                        Radius.circular(15),
                      ),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Color(0x00000000)),
                      borderRadius: BorderRadius.all(
                        Radius.circular(15),
                      ),
                    ),
                  ),
                ),
                width: 340,
              )
            ),
            //  注册按钮
            SizedBox(height: 10,),
            ElevatedButton(
              child: new Text('注册'),
              onPressed: () {
                if(pwd2.text != pwd.text){
                  showDialog<Null>(
                      context: context,
                      barrierDismissible: false,
                      builder: (BuildContext context) {
                        return new AlertDialog(
                          content: new SingleChildScrollView(
                            child: new ListBody(
                              children: <Widget>[
                                new Text('两次密码输入不一致'),
                              ],
                            ),
                          ),
                          actions: <Widget>[
                            new ElevatedButton(
                              child: new Text('确定'),
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                            ),
                          ],
                        );
                      });
                }
                else{
                  qqId = id.text;
                  qqname = name.text;
                  qqpwd = pwd2.text;
                  qqpic = picUrl;
                  print('qqname:'+qqname);
                  print('qqPwd:'+qqpwd);
                  queryDatabase(context);
                }
              },
            ),
          ],
        ),
      ),
    );
  }
  queryDatabase(BuildContext context) async {
    TableTestProvider testProvider = TableTestProvider();
    //  通过getDatabasePath()方法获取数据库位置
    var databasePath = await getDatabasesPath();
    String path = join(databasePath, 'demo.db');
    // deleteDatabase(path);
    // await testProvider.create(path);
    await testProvider.open(path);
    // // 查询数据
    Test dd = await testProvider.getTodo(qqId);
    print('查询${qqId}:${dd.toMap()}');
    if(dd.id!=null){
      print('qq号已存在');
      Fluttertoast.showToast(msg: 'QQ号已存在',textColor: Colors.white);
    }
    else{
      // 新增注册数据
      Test test1 = Test();
      test1.id = qqId;
      test1.name = qqname;
      test1.pwd = qqpwd;
      test1.pic = qqpic;
      Test td = await testProvider.insert(test1);
      print('注册成功:${td.toMap()}');
      Fluttertoast.showToast(msg: '注册成功！！',textColor: Colors.white);

      Navigator.of(context)
          .push(MaterialPageRoute(builder: (BuildContext context) {
        return Login();
      }));
    }
    // 关闭数据库
    testProvider.close();
  }

}